/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.BuyyerDao;
import com.taobao.snowsnow.hibernate.pojo.Buyyer;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class BuyyerDaoImpl extends BaseDaoImpl implements BuyyerDao {
	
	public Buyyer findById(int id) throws Exception {
		return (Buyyer) getHibernateTemplate().get(Buyyer.class, id);
	}
	
	public List<Buyyer> findByName(String name) throws Exception {
		return super.findByProperty(Buyyer.class, "name", name);
	}

	public List<Buyyer> findAll() throws Exception {
		return getHibernateTemplate().find("from Buyyer");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Buyyer.class);
	}

	public List<Buyyer> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Buyyer.class, page, limit);
	}

	@Override
	public Buyyer getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Buyyer as o where o.id = (select min(id) from Buyyer)");
		List<Buyyer> list = query.list();
		if(list.size() !=0)
			return (Buyyer) list.get(0);
		else return null;
	}

	@Override
	public Buyyer getLastObject() throws Exception {
		Query query = getSession().createQuery("from Buyyer as o where o.id = (select max(id) from Buyyer)");
		List<Buyyer> list = query.list();
		if(list.size() !=0)
			return (Buyyer) list.get(0);
		else return null;
	}

	@Override
	public Buyyer findByNamePrecise(String name) throws Exception {
		List<Buyyer> list = findByName(name);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public List<Buyyer> findByTaoBaoID(String nick) throws Exception {
		Query query = getSession().createQuery("from Buyyer as b where b.taobaoId = :nick");
		query.setString("nick", nick);
		return query.list();
	}

	@Override
	public Buyyer findByTaoBaoIDPrecise(String nick) throws Exception {
		List<Buyyer> list = findByTaoBaoID(nick);
		if(list.size() !=0)
			return (Buyyer) list.get(0);
		else return null;
	}
}
